package com.learn.project2.service.wx.topic;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.mapper.MarketTopicMapper;
import com.learn.project2.model.vo.wx.topic.TopicDetailVO2;
import com.learn.project2.model.vo.wx.topic.TopicListVO2;
import com.learn.project2.model.vo.wx.topic.TopicRelatedVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 曹源
 * @version 1.0
 * @description: 微信小程序 /wx/topic的接口方法实现
 * @date 2022/1/10 17:36
 */
@Service
public class TopicServiceImpl2 implements TopicService2 {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    /**
     * @description: 微信小程序 /wx/topic/list的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-10 18:59
     */
    @Override
    public TopicListVO2 selectList(Integer page, Integer limit) {
        TopicListVO2 topicListVO2 = new TopicListVO2();
        List<TopicListVO2.ListDTO> listDTOS = new ArrayList<>();
        //分页插件
        PageHelper.startPage(page,limit);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.setOrderByClause("add_time");
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        PageInfo<MarketTopic>marketTopicPageInfos = new PageInfo<MarketTopic>(marketTopics);
        for (MarketTopic marketTopic : marketTopics) {
            TopicListVO2.ListDTO listDTO = new TopicListVO2.ListDTO();
            listDTO.setId(marketTopic.getId());
            listDTO.setTitle(marketTopic.getTitle());
            listDTO.setSubtitle(marketTopic.getSubtitle());
            listDTO.setPrice(marketTopic.getPrice());
            listDTO.setReadCount(marketTopic.getReadCount());
            listDTO.setPicUrl(marketTopic.getPicUrl());
            listDTOS.add(listDTO);
        }
        topicListVO2.setPage(page);
        topicListVO2.setLimit(limit);
        topicListVO2.setPages(marketTopicPageInfos.getPages());
        topicListVO2.setTotal((int) marketTopicPageInfos.getTotal());
        topicListVO2.setList(listDTOS);
        return topicListVO2;
    }

    /**
     * @description: 微信小程序 /wx/topic/detail的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-10 19:58
     */
    @Override
    public TopicDetailVO2 selectDetail(Integer id) {
        TopicDetailVO2 topicDetailVO2 = new TopicDetailVO2();

        List<TopicDetailVO2.GoodsDTO> goodsDTOs = new ArrayList<>();
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);
        MarketTopic marketTopic=marketTopics.get(0);
        topicDetailVO2.setTopic(marketTopic);
        //得到goodsid查询
        Integer[] goods = marketTopic.getGoods();
        for (Integer good : goods) {
            TopicDetailVO2.GoodsDTO goodsDTO = new TopicDetailVO2.GoodsDTO();
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
            criteria1.andDeletedEqualTo(false);
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(good);
            goodsDTO.setId(marketGoods.getId());
            goodsDTO.setName(marketGoods.getName());
            goodsDTO.setBrief(marketGoods.getBrief());
            goodsDTO.setPicUrl(marketGoods.getPicUrl());
            goodsDTO.setIsNew(marketGoods.getIsNew());
            goodsDTO.setIsHot(marketGoods.getIsHot());
            goodsDTO.setCounterPrice(marketGoods.getCounterPrice());
            goodsDTO.setRetailPrice(marketGoods.getRetailPrice());
            goodsDTOs.add(goodsDTO);
        }
        topicDetailVO2.setGoods(goodsDTOs);
        return topicDetailVO2;
    }

    /**
     * @description: 微信小程序 /wx/topic/Related的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-10 20:21
     */
    @Override
    public TopicRelatedVO2 selectRelated(Integer id) {
        TopicRelatedVO2 topicRelatedVO2 = new TopicRelatedVO2();
        //查询传入的id后的四条数据作为相关推荐
        Integer page = 1;
        Integer limit = 4;
        //分页插件，分页
        PageHelper.startPage(page, limit);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdGreaterThan(id).andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);
        PageInfo<MarketTopic> marketTopicsPageinfos = new PageInfo<>(marketTopics);
        topicRelatedVO2.setPage(page);
        topicRelatedVO2.setLimit(limit);
        topicRelatedVO2.setPages(marketTopicsPageinfos.getPages());
        topicRelatedVO2.setTotal((int) marketTopicsPageinfos.getTotal());
        topicRelatedVO2.setList(marketTopics);
        return topicRelatedVO2;
    }
}
